package com.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.R;
import com.edu.commonutils.Results;
import com.edu.eduservice.domain.EduChapter;
import com.edu.eduservice.domain.EduVideo;
import com.edu.eduservice.domain.chapter.ChapterVo;
import com.edu.eduservice.mapper.EduChapterMapper;
import com.edu.eduservice.mapper.EduVideoMapper;
import com.edu.eduservice.service.EduChapterService;
import com.edu.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //章节mapper注入
    @Autowired(required = false)
    private EduChapterMapper eduChapterMapper;

    //小节mapper注入
    @Autowired(required = false)
    private EduVideoMapper eduVideoMapper;


    //拿到所有的章节小节
    @Override
    public R getChapterService(String courseid) {
        //查询所有courseid章节
        QueryWrapper<EduChapter> queryWrapperchapter=new QueryWrapper();
        queryWrapperchapter.eq("course_id",courseid);
        List<EduChapter> eduChapters = this.eduChapterMapper.selectList(queryWrapperchapter);

        //查询所有小节
        QueryWrapper<EduVideo> queryWrappervideo=new QueryWrapper();
        queryWrappervideo.eq("course_id",courseid);
        List<EduVideo> eduVideos = this.eduVideoMapper.selectList(queryWrappervideo);
        
        List<ChapterVo> chapterVos=new ArrayList<>();
        //遍历所有章节
        for (EduChapter educhapter : eduChapters) {
            //创建章节 默认赋值防止为空 空指针异常
            ChapterVo chapterVo = new ChapterVo(educhapter.getId(), educhapter.getTitle(),new ArrayList<>());
            //循环所有的小节 找到对应的小节放到章节里面去
            for (EduVideo eduVideo : eduVideos) {
                //判断是哪个章节下面的
                if(eduVideo.getChapterId().equals(educhapter.getId())){
                    ChapterVo chapterVo1 = new ChapterVo(eduVideo.getId(), eduVideo.getTitle());
                    chapterVo1.setVideoSourcesId(eduVideo.getVideoSourceId());
                    chapterVo1.setIsFree(eduVideo.getIsFree());
                    //添加进入章节里面
                    chapterVo.getChildren().add(chapterVo1);
                }
            }
            //所有的章节添加进入大纲
            chapterVos.add(chapterVo);
        }
        //封装数据
        R r = R.ok().data("chapters",chapterVos);
        return r;
    }

    @Override
    public boolean delChapterService(String id) throws MyException {
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",id);
        Integer count = this.eduVideoMapper.selectCount(wrapper);
        if(count>0){
            throw new MyException(Results.ERROR,"请先删除小节");
        }else {
            int i = this.eduChapterMapper.deleteById(id);
            return i>0;
        }
    }
}
